$(document).ready(function(){
	var type=$('#type').val();
	var costId=$('#costId').val();
	genDictSelect('positionCode','position','1');
	initForm(type,costId);
	//事件绑定
	eventBind(type,costId);
});
//初始化form表单
function initForm(type,costId){
	$('#costForm1').validation();
	if(costId==''){
		//初始化所属医院级拉框
		initHospitalSel(null,null,null);
	}else{
		$.getApiClient('/cost/select/{costId}').setQuery({costId: costId}).get().done(function(data){
			if(data.code=='0'){
				var payload=data.payload;
				$('#costForm1').bindData(payload);
				//初始化所属医院级拉框
				initHospitalSel(payload.hospital_id,payload.department_id,payload.doc_id);
			}
		})
	}
}
//事件绑定
function eventBind(type,costId){
	$('#cancel').click(function(){
		BootstrapDialog.closeAll();
	})
	if(costId==''){
		$('#save').on('click',saveNewCost);
	}else{
		$('#save').on('click',function(){
			updateCost(costId);
		});
	}
}
//查询医院
function initHospitalSel(defaultHosVal,defaultDeptsVal,defaultDocVal){
	$.getApiClient('/baseInfo/hospitals').get().done(function(data){
		if(!data.payload){
			data.payload=[];
		}
	    multipleSelectData("hospital",[{"id": "", "hospital_name": "请选择"}].concat(data.payload), defaultHosVal, {valueAttr:'id', displayAttr:'hospital_name'},'请选择');
		$('#hospital').multipleSelect({width:'100%', single: true});
		
		//医院科室下拉框联动
		$('#hospital').change(function(){
			var hosid=$(this).children('option:selected').val();
			initDepartmentSel(hosid,defaultDeptsVal,defaultDocVal);
		}).change();
	});
}

//初始化所属科室下拉框
function initDepartmentSel(hosId,defaultVal,defaultDocVal){
	if(hosId){
		$.getApiClient('/baseInfo/departments').setQuery({hospital_id:hosId}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("department",[{"id": "", "department_name": "请选择"}].concat(data.payload), defaultVal, {valueAttr:'id', displayAttr:'department_name'},"请选择");
			$('#department').multipleSelect({width:'100%', single: true});
		});
		initAssignDocSel(defaultVal,defaultDocVal);
	}else{
		multipleSelectData("department",[{"id": "", "department_name": "请选择"}], defaultVal, {valueAttr:'id', displayAttr:'department_name'},"请选择");
		$('#department').multipleSelect({width:'100%', single: true});
		//科室医生下拉框联动
		$('#department').change(function(){
			var deptId=$(this).children('option:selected').val();
			initAssignDocSel(deptId,defaultDocVal);
		}).change();
	}
}

//初始化指定医生下拉框
function initAssignDocSel(deptId,defaultDocVal){
	if(deptId){
		$.getApiClient('/consultation/doctors/{department_id}').setQuery({department_id:deptId}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("user",[{"id": "", "user_name": "请选择"}].concat(data.payload), defaultDocVal, {valueAttr:'id', displayAttr:'user_name'},"请选择");
			$('#user').multipleSelect({width:'100%', single: true});
		});
	}else{
		multipleSelectData("user",[{"id": "", "user_name": "请选择"}], defaultDocVal, {valueAttr:'id', displayAttr:'user_name'},"请选择");
		$('#user').multipleSelect({width:'100%', single: true});
	}
}

//新增用户操作
function saveNewCost(){
	if(!$("#costForm1").validationForm()){
		return;
	}
	$.getApiClient('/cost/save').post('#costForm1', function(params){
		return $.extend(params, {
			position_name: $('#position').multipleSelect('getSelects','text')[0],
			doc_id: $('#user').multipleSelect('getSelects','text')[1]
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','保存成功',null,2000);
			pageQuery();
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}

//修改用户操作
function updateCost(costId){
	if(!$("#costForm1").validationForm()){
		return;
	}
	$.getApiClient('/cost/update/{id}').setQuery({
		id:costId
	}).put('#costForm1', function(params){
		return $.extend(params, {
			position_name: $('#position').multipleSelect('getSelects','text')[0],
			doc_id: $('#user').multipleSelect('getSelects','text')[1]
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','修改成功',null,2000);
			pageQuery();
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}